<?php 
require("gConfig.php");
$uid = $_GET['uid'];
if ( $uid == "" )
{
    exit( );
}
checkuid($uid,5);
$bet = $_GET["bet"];
$type = $_GET["type"];
$errstr = "";
if($type == 8) {
	unBet();
}
if( isset( $bet ) && isset( $type )){
	updateBet( $bet,$type );
}

$query = "select uCredit from m_users where uName=".$MyData['Name']." limit 1 ";
$result = mysql_query($query,$comm);
$row = mysql_fetch_row($result);
$uCredit = $row[0];
if(strlen($errstr) == 0)
{
	echo "{
		\"state\":\"$type\",
		\"state\":\"true\",
		\"ErrorTip\":\"\",
		\"uCredit\":\"{$uCredit}\"
		}";
}else{
	echo "{
		\"state\":\"$type\",
		\"state\":\"false\",
		\"ErrorTip\":\"{$errstr}\",
		\"uCredit\":\"{$uCredit}\"
		}";
}

function updateBet( $bet,$type )
{
    global $HOSTIP;
    global $CFG_MyIP;
    global $CFG_MyName;
    global $CFG_MyPasswd;
    global $CFG_MyDb;
    global $MyData;
    global $errstr;
    
    if ($type <0) return ;
    if ($type >=7) return ;
    if ($bet<=0) return ;
    
    $config = getBetConfig($MyData['Name']);
    
    $comm1 = mysql_connect( @$CFG_MyIP, @$CFG_MyName, @$CFG_MyPasswd )or die ('Can\'t use peng : ' . mysql_error());
    mysql_select_db( "livebac_rb", $comm1 )or die ('Can\'t use peng : ' . mysql_error());
    
    $sqlstr = "select gGame1,gState from b_group where gName='{$MyData['Name']}' ";
    $result = mysql_query($sqlstr,$comm1);
    $row = mysql_fetch_row($result);
    if($row[1]&2){
    	$errstr ="账号被锁定，不能下注！";
    	return;
    }
//     $games = explode(",", $row[0]);
//     $backwater = $games[0];
//     $minBet = $games[1];
//     $maxBet = $games[2];
//     $maxEQBet = $games[3];
//     $maxDouBet = $games[4];
//     $maxDXBet = $games[5];
    //开始事务
    mysql_query("BEGIN",$comm1); 
    $sqlstr = "select uBets,uCredit,uId,gTree from m_users where uName=".$MyData['Name']." limit 1 for update";
//    error_log("select uBets from m_users ....sqlstr is:".print_r($sqlstr,true));
    $result = mysql_query($sqlstr,$comm1);
    $row = mysql_fetch_row($result);
//    error_log("updateBet log row is:".print_r($row,true));
	$room = $row[2];
	$uCredit = $row[1];
	$tree = explode(",", $row[3]);
    $uBets = explode(",", $row[0]);
    $uBets[$type] = (string)((int)$uBets[$type] + $bet);
    $uBetstr = $uBets[0].",".$uBets[1].",".$uBets[2].",".$uBets[3].",".$uBets[4].",".$uBets[5].",".$uBets[6];
// 	$allbet = 0;
//     for($i=0;$i<count($uBets);$i++){
//     	$allbet += (int)$uBets[$i];
//     }
    $sqlstr = "update m_users set uBets='$uBetstr' where uName=".$MyData['Name']." and uId='{$room}'";
//    error_log("update m_users set uBets ....sqlstr is:".print_r($sqlstr,true));
    mysql_query($sqlstr,$comm1);
    //更新总状态表
    $sqlstr = "select sBets,sState from m_state where sId='{$room}' for update ";
    $result = mysql_query($sqlstr,$comm1);
    $row = mysql_fetch_row($result);
    $roomstate = $row[1];
    if($roomstate != 1){
    	mysql_query("rollback",$comm1);
    	$errstr ="现在还不能下注！";
    	return ;
    }
    $sBets = explode(",", $row[0]);
    $sBets[$type] = (string)((int)$sBets[$type] + $bet);
    $sBetstr = $sBets[0].",".$sBets[1].",".$sBets[2].",".$sBets[3].",".$sBets[4].",".$sBets[5].",".$sBets[6];
    $sqlstr = "update m_state set sBets='$sBetstr' where sId='{$room}'";
//    error_log("update m_users set uBets ....sqlstr is:".print_r($sqlstr,true));
    mysql_query($sqlstr,$comm1);
    
    $allbet = 0;
    for($i=0;$i<count($uBets);$i++){
    	$allbet += (int)$uBets[$i];
    }
    if($allbet > $uCredit){
    	mysql_query("rollback",$comm1);
    	$errstr ="您的积分不足！";
    }
    //判断下注是否合理
    if (abs($sBets[0]-$sBets[1])>$betconfig["nobet3"]){
    	mysql_query("rollback",$comm1);
    	$errstr= "庄闲差距范围是：{$betconfig["nobet3"]},不能超过该范围。";
    }
    if ($type<=1){
    	if ($uBets[$type]>=$config["minbet"] && $uBets[$type]<=$config["maxbet1"]){
    		mysql_query("COMMIT",$comm1);
    	}else{
    		mysql_query("rollback",$comm1);
    		$errstr ="庄闲下注范围是:{$config["minbet"]}--{$config["maxbet1"]},不能超过该范围！";
    	}
    }else if($type == 2){
    	if ($uBets[$type]>=$config["minbet"] && $uBets[$type]<=$config["maxbet2"]){
    		mysql_query("COMMIT",$comm1);
	    }else{
	    	mysql_query("rollback",$comm1);
	    	$errstr ="和局下注范围是:{$config["minbet"]}--{$config["maxbet2"]},不能超过该范围！";
	    }
    }else if($type == 3 || $type == 4){
    	if ($uBets[$type]>=$config["minbet"] && $uBets[$type]<=$config["maxbet3"]){
    		mysql_query("COMMIT",$comm1);
	    }else{
	    	mysql_query("rollback",$comm1);
	    	$errstr ="押对下注范围是:{$config["minbet"]}--{$config["maxbet3"]},不能超过该范围！";
	    }
    }else if($type == 5 || $type == 6){
    	if ($uBets[$type]>=$config["minbet"] && $uBets[$type]<=$config["maxbet4"]){
    		mysql_query("COMMIT",$comm1);
	    }else{
	    	mysql_query("rollback",$comm1);
	    	$errstr ="押大小下注范围是:{$config["minbet"]}--{$config["maxbet4"]},不能超过该范围！";
	    }
    }else{
		mysql_query("rollback",$comm1);
	    $errstr ="不存在的下注种类";
    }
}

function unBet( )
{
    global $HOSTIP;
    global $CFG_MyIP;
    global $CFG_MyName;
    global $CFG_MyPasswd;
    global $CFG_MyDb;
    global $MyData;
    global $errstr;
    $config = getBetConfig($MyData['Name']);
    
    $comm1 = mysql_connect( @$CFG_MyIP, @$CFG_MyName, @$CFG_MyPasswd )or die ('Can\'t use peng : ' . mysql_error());
    mysql_select_db( "livebac_rb", $comm1 )or die ('Can\'t use peng : ' . mysql_error());
    
    $sqlstr = "select gGame1,gState from b_group where gName='{$MyData['Name']}' ";
    $result = mysql_query($sqlstr,$comm1);
    $row = mysql_fetch_row($result);
    if($row[1]&2){
    	$errstr ="账号被锁定，不能下注！";
    	return;
    }
//    $games = explode(",", $row[0]);
//    $backwater = $games[0];
//    $minBet = $games[1];
//    $maxBet = $games[2];
//    $maxEQBet = $games[3];
//    $maxDouBet = $games[4];
//    $maxDXBet = $games[5];
    //开始事务
    mysql_query("BEGIN",$comm1); 
    $sqlstr = "select uBets,uId from m_users where uName=".$MyData['Name']." limit 1 for update";
//    error_log("select uBets from m_users ....sqlstr is:".print_r($sqlstr,true));
    $result = mysql_query($sqlstr,$comm1);
    $row = mysql_fetch_row($result);
//    error_log("updateBet log row is:".print_r($row,true));
	$room = $row[1];
    $uBets = explode(",", $row[0]);
    $uBetstr = "0,0,0,0,0,0,0";

    $sqlstr = "update m_users set uBets='$uBetstr' where uName=".$MyData['Name']." and uId='{$room}'";
//    error_log("update m_users set uBets ....sqlstr is:".print_r($sqlstr,true));
    mysql_query($sqlstr,$comm1);
    //更新总状态表
    $sqlstr = "select sBets,sState from m_state where sId='{$room}' for update ";
    $result = mysql_query($sqlstr,$comm1);
    $row = mysql_fetch_row($result);
    $roomstate = $row[1];
    if($roomstate != 1){
    	mysql_query("rollback",$comm1);
    	$errstr ="现在还不能下注！";
    	return ;
    }
    $sBets = explode(",", $row[0]);
    for($i=0;$i<count($uBets);$i++){
    	$sBets[$i] = (string)($sBets[$i] - $uBets[$i]);
    }
    $sBetstr = $sBets[0].",".$sBets[1].",".$sBets[2].",".$sBets[3].",".$sBets[4].",".$sBets[5].",".$sBets[6];
    $sqlstr = "update m_state set sBets='$sBetstr' where sId='{$room}'";
    mysql_query($sqlstr,$comm1);
    
    mysql_query("COMMIT",$comm1);
}

function getBetConfig($name){
	global $comm;
	
	$query = "select gTree,gGame1 from b_group where gName={$name}";
	$result = mysql_query($query,$comm);
	$row = mysql_fetch_row($result);
	$tree = explode(",", $row[0]);
	$betboss = $tree[1];
	$ss = explode(",", $row[1]);
	
	$query = "select gGame1 from b_group where gName={$betboss} and gLevel = 1 ";
	$result = mysql_query($query,$comm);
	$row = mysql_fetch_row($result);
	$s = explode( ",", $row[0] );
	$betconfig["water"] = $s[0];
	$betconfig["minbet"] = $s[1];//最小 
	$betconfig["maxbet1"] = $s[2];//庄闲最大
	$betconfig["maxbet2"] = $s[3];//和局最大
	$betconfig["maxbet3"] = $s[4];//对子最大
	$betconfig["limit1"] = $s[5];//群组限押设定 百家乐 没有使用
	$betconfig["limit2"] = $s[6];
	$betconfig["limit3"] = $s[7];
	$betconfig["room"] = $s[8];
	$betconfig["betmode"] = $s[12];//总投.胜点
	$betconfig["mag1"] = $s[13];//庄闲倍数
	$betconfig["mag2"] = $s[14];//和局倍数
	$betconfig["mag3"] = $s[15];//对子倍数
	$betconfig["nobet1"] = $s[16];//未押次数
	$betconfig["nobet2"] = $s[17];//可押倍数
	$betconfig["nobet3"] = $s[18];//庄闲差距
	$betconfig["nobet4"] = $s[19];//差距次数
	$betconfig["nobet5"] = $s[20];//可押倍数
	$betconfig["maxbet4"] = $s[21];
	$betconfig["limit4"] = $s[22];
	$betconfig["mag4"] = $s[23];
	
	$betconfig["water"] = $ss[0];
	$betconfig["maxbet1"] = $ss[2];//庄最大
	$betconfig["maxbet2"] = $ss[3];
	$betconfig["maxbet3"] = $ss[4];
	$betconfig["maxbet4"] = $ss[5];
	
	return $betconfig;
}
?>